package com.spsy.orders.controller;

import com.alibaba.fastjson2.JSON;
import com.spsy.common.core.domain.R;
import com.spsy.common.core.web.page.TableDataInfo;
import com.spsy.common.vo.OrderReq;
import com.spsy.orders.domain.Orders;
import com.spsy.orders.service.IOrdersService;
import com.spsy.orders.service.OrderOptsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: liuzhiping
 * @date: 2025/2/11 16:34
 **/
@RestController
@RequestMapping("/orders")
public class OrdersOptController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IOrdersService ordersService;

    @Autowired
    private com.spsy.orders.service.OrderOptsService orderOptsService;
    /**
     * 查询订单s列表
     */
    @GetMapping("/topten")
    public R list(Orders orders)
    {
        String key = "spsy:order:total:amount:top:ten";
        List<Orders> list = JSON.parseObject((String)redisTemplate.opsForValue().get(key),List.class);
        return R.ok(list);
    }



    @GetMapping("/getOrdersByOrderId")
    public R getOrders(@RequestParam String orderId)
    {

        Map<String,String> map = new HashMap<>();

        map.put("1","1");

        String mapKey = "spsy:order:total:amount:top:ten:map";

        String value = (String)this.redisTemplate.opsForHash().get(mapKey,orderId);

        Orders orders = JSON.parseObject(value,Orders.class);


        return R.ok(orders);
    }



    /***
     * 返回二维码给用户
     *  {@link OrderReq } 订单请求参数对象
     * @return
     */
    @RequestMapping("/doOrder")
    public R createOrder(@RequestBody OrderReq orderReq){
        System.out.println("========doOrder 下单开始 orderId="+ JSON.toJSONString(orderReq));

        R r = this.orderOptsService.execOrder(orderReq);

        return r;
    }

}
